from sqlite3 import connect
import config

SUBSCRIBE_SQL = 'insert or replace into %s (%s, %s, %s, %s) values (?, ?, ?, ?)' \
		%(config.STOCK_TBL, config.FETION_URI, \
		  config.STOCK_ID, config.HIGH_PRICE, config.LOW_PRICE) 

class Subscriber(object):
	def __init__(self, uri):
		self.__uri = uri
		self.__conn = connect(config.DATABASE)
		self.__cur = self.__conn.cursor()
	def __del__(self):
		self.__cur.close()
	def subscribe(self, stockid, high_price, low_price):
		self.__cur.execute(SUBSCRIBE_SQL, (self.__uri, stockid, high_price, low_price))
		self.__conn.commit()

#syncdb is dropped in current version
def syncdb():
	mobile_no_list = []
	fetion_uris, fetion_ids = share.fetion.get_contact_list()
	for uri in fetion_uris:
		mobile_no = share.fetion.get_contact_info(uri)
		mobile_no_list.append(mobile_no)
	assert len(fetion_uris) == len(mobile_no_list)
	assert len(fetion_uris) == len(fetion_ids)
	conn = connect(config.DATABASE)
	cur = conn.cursor()
	fetion_uris_num = len(fetion_uris)
	#TODO: delete all the data items in fetiontbl
	#for i in range(fetion_uris_num):
	#	cur.execute('insert or replace into fetiontbl values (?, ?, ?)', (fetion_ids[i], mobile_no_list[i], fetion_uris[i]))
	#conn.commit()
	cur.close()

def conv2fetionid(login_id): 
	return login_id #TODO

if __name__ == "__main__":
	#syncdb()
	scrber2 = Subscriber('793401629@fetion.com.cn;p=1919') 
	scrber2.subscribe('002008', 11.97, 11.68)
